from jinja2 import Template
from common.collector_common.receiver import LogstashInputTPL, LogstashInit
from model.model import Source


class ReceiverOpration:
    """接收器 对应服务操作"""
    Category = ''

    @classmethod
    def operation(cls, item):
        if item.enabled is False:
            return LogstashInit
        
        template = Template(LogstashInputTPL)
        return template.render(category=item.Category, port=item.port, _receiver_id=item.id)


class APIServer(ReceiverOpration):
    Category = 'apiserver'

    def operation(item):
        pass  # go|python api接收


class Syslog(ReceiverOpration):
    Category = 'syslog'


class TCP(ReceiverOpration):
    Category = 'tcp'


class UCP(ReceiverOpration):
    Category = 'udp'


receiver_map = {item.Category: item for item in ReceiverOpration.__subclasses__()}


class ReceiverView(ReceiverConfigure):

    @classmethod
    def enable_receiver(cls, session, filter_, update):
        item = cls.update_item(session=session, filter_=filter_, update=update)
        receiver_map[item.category].operation(item)
        return item
